package com.liukai.todo_android.ui.activity;


import android.content.Context;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.widget.AppCompatButton;

import com.liukai.todo_android.R;
import com.liukai.todo_android.common.utils.ValidateUtil;
import com.liukai.todo_android.common.base.BaseActivity;
import com.liukai.todo_android.call_back.IRegisterCallBack;
import com.liukai.todo_android.entity.vo.LoginVO;
import com.liukai.todo_android.entity.Resp;
import com.liukai.todo_android.entity.vo.RegisterVO;
import com.liukai.todo_android.presenter.RegisterPresenter;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import butterknife.BindView;

public class RegisterActivity extends BaseActivity implements IRegisterCallBack {

    @BindView(R.id.et_email)
    EditText etEmail;
    @BindView(R.id.et_password)
    EditText etPassword;
    @BindView(R.id.et_confirm)
    EditText etConfirm;
    @BindView(R.id.et_verification)
    EditText etVerification;
    @BindView(R.id.btn_getCode)
    TextView btnGetCode;
    @BindView(R.id.btn_Register)
    AppCompatButton btnRegister;
    @BindView(R.id.btn_back)
    TextView btnBack;
    @BindView(R.id.btn_reset)
    AppCompatButton btnReset;

    private RegisterPresenter registerPresenter;
    private Context context;
    private ExecutorService executorService;


    @Override
    protected int getLayoutId() {
        return R.layout.activity_register;
    }

    @Override
    protected void initData(Context _context) {
        registerPresenter = new RegisterPresenter();
        registerPresenter.registerCallback(this);
        context = _context;
        executorService = Executors.newFixedThreadPool(1);
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void initListener() {
        // 获取验证码
        btnGetCode.setOnClickListener(v -> {
            String email = etEmail.getText().toString().trim();
            if (ValidateUtil.canGetVerificationCode(email, context)) {
                etEmail.setEnabled(false);
                registerPresenter.getCode(email);
            }

        });
        // 登录
        btnRegister.setOnClickListener(v -> {
            String email = etEmail.getText().toString().trim();
            String password = etPassword.getText().toString().trim();
            String confirm = etConfirm.getText().toString().trim();
            String verificationCode = etVerification.getText().toString().trim();
            // 校验数据格式是否满足登录条件
            if (ValidateUtil.canRegister(email, password, confirm, verificationCode, context)) {
                RegisterVO registerVO = new RegisterVO();
                registerVO.setEmail(email);
                registerVO.setPassword(password);
                registerVO.setCode(verificationCode);
                registerPresenter.register(registerVO);
            }
        });
        // 返回
        btnBack.setOnClickListener(v -> {
            RegisterActivity.this.finish();
        });
        // 重置
        btnReset.setOnClickListener(v -> {
            resetForm();
        });
    }

    private void fun1() {

        executorService.submit(new Runnable() {
            @Override
            public void run() {

            }
        });

    }


    @Override
    protected void release() {
        resetForm();
    }

    private void resetForm() {
        etConfirm.setText("");
        etEmail.setText("");
        etPassword.setText("");
        etVerification.setText("");
        etEmail.setEnabled(true);
    }

    @Override
    public void getCodeSuccess(Resp resp) {
        Toast.makeText(context, resp.getMessage(), Toast.LENGTH_SHORT).show();
    }

    @Override
    public void getCodeError(Resp resp) {
        Toast.makeText(context, resp.getMessage(), Toast.LENGTH_SHORT).show();
        etEmail.setEnabled(true);
    }

    @Override
    public void registerSuccess(Resp resp) {
        Toast.makeText(context, resp.getMessage(), Toast.LENGTH_SHORT).show();
        RegisterActivity.this.finish();
    }

    @Override
    public void registerError(Resp resp) {
        String email = etEmail.getText().toString();
        Toast.makeText(context, resp.getMessage(), Toast.LENGTH_SHORT).show();
        resetForm();
        int code = resp.getCode();
        if (code == 501 || code == 502 || code == 503 || code == 504) {
            etEmail.setText(email);
        }
    }
}